//
//  MeViewController.m
//  DLBaseProject
//
//  Created by xiedonglai on 2021/7/19.
//  Copyright © 2021 XDL. All rights reserved.
//

#import "MeViewController.h"
#import "MyTableViewCell.h"
#import "SelectHeadView.h"
#import "HelpViewController.h"
#import "IntroduceViewController.h"
#import "ClearDataViewController.h"
#import "ContactViewController.h"

@interface MeViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIView *headView;
@property (strong, nonatomic) UIView *footView;
@property (strong, nonatomic) UIButton *headImg;
@property (strong, nonatomic) UILabel *nameLab;
@property (strong, nonatomic) UIButton *timeLab;

@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.title = @"";
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.headImg setImage:[UIImage imageNamed:[NSString stringWithFormat:@"head%ld.jpeg", [DataManager shareManager].dataModel.headIndex+1]] forState:(UIControlStateNormal)];
    self.nameLab.text = [DataManager shareManager].dataModel.name.length > 0 ? [DataManager shareManager].dataModel.name : @"WebPageSaver";
}

#pragma mark - tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = [MyTableViewCell cellForMyWithTableView:self.tableView indentifier:@"cell" style:(UITableViewCellStyleValue1)];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    if (indexPath.row == 0) {
        cell.cellStyle = BaseCellStyleFirst;
        cell.imageView.image = [UIImage imageNamed:@"jieshao.png"];
        cell.lab.text = @"介绍";
    } else if (indexPath.row == 1) {
        cell.cellStyle = BaseCellStyleMiddle;
        cell.imageView.image = [UIImage imageNamed:@"bangzhu.png"];
        cell.lab.text = @"帮助";
    } else if (indexPath.row == 2) {
        cell.cellStyle = BaseCellStyleMiddle;
        cell.imageView.image = [UIImage imageNamed:@"lianxi.png"];
        cell.lab.text = @"联系我们";
    } else if (indexPath.row == 3) {
        cell.cellStyle = BaseCellStyleMiddle;
        cell.imageView.image = [UIImage imageNamed:@"shuju.png"];
        cell.lab.text = @"数据管理";
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {//介紹
        IntroduceViewController *v = [[IntroduceViewController alloc] init];
        [self.navigationController pushViewController:v animated:YES];
    } else if (indexPath.row == 1) {//幫助
        HelpViewController *v = [[HelpViewController alloc] init];
        [self.navigationController pushViewController:v animated:YES];
    } else if (indexPath.row == 2) {//聯繫我們
        ContactViewController *v = [[ContactViewController alloc] init];
        [self.navigationController pushViewController:v animated:YES];
    } else if (indexPath.row == 3) {//數據管理
        ClearDataViewController *v = [[ClearDataViewController alloc] init];
        [self.navigationController pushViewController:v animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *v = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenW, Height_Real(10)))];
    return v;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return Height_Real(65);
}

#pragma mark -點擊事件
- (void)clickHead {
    SelectHeadView *v = [[SelectHeadView alloc] init];
    WeakSelf(weakSelf)
    [v show:^{
        [weakSelf.headImg setImage:[UIImage imageNamed:[NSString stringWithFormat:@"head%ld.jpeg", [DataManager shareManager].dataModel.headIndex+1]] forState:(UIControlStateNormal)];
    }];
}

//- (void)clickName {
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请输入昵称" message:@"" preferredStyle:UIAlertControllerStyleAlert];
//    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
//    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        UITextField *userNameTextField = alertController.textFields.firstObject;
//        [DataManager shareManager].dataModel.name = userNameTextField.text;
//        self.nameLab.text = userNameTextField.text;
//        [[DataManager shareManager] saveData];
//    }]];
//    [alertController addTextFieldWithConfigurationHandler:^(UITextField*_Nonnull textField) {
//        textField.placeholder = @"请输入昵称";
//
//    }];
//    [self presentViewController:alertController animated:YES completion:nil];
//}

- (void)clickLogOut {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否退出登录？" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        RegisterViewController *v = [[RegisterViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:v];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [self.navigationController presentViewController:nav animated:YES completion:^{
            self.tabBarController.selectedIndex = 0;
        }];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:okAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:true completion:nil];
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:(UITableViewStylePlain)];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headView;
        _tableView.tableFooterView = self.footView;
    }
    return _tableView;
}

- (UIView *)headView {
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenW, Height_Real(190)))];
        _headView.backgroundColor = [AppColor getColor_MAIN];
        
        self.headImg = [[UIButton alloc] init];
//        [self.headImg setImage:[UIImage imageNamed:[NSString stringWithFormat:@"head%ld.png", [DataManager shareManager].dataModel.headIndex+1]] forState:(UIControlStateNormal)];
        self.headImg.layer.cornerRadius = Width_Real(50);
        self.headImg.layer.borderWidth = Width_Real(3);
        self.headImg.layer.borderColor = [UIColor whiteColor].CGColor;
        self.headImg.layer.masksToBounds = YES;
        [self.headImg addTarget:self action:@selector(clickHead) forControlEvents:(UIControlEventTouchUpInside)];
        [_headView addSubview:self.headImg];
        [self.headImg autoSetDimensionsToSize:(CGSizeMake(Width_Real(100), Width_Real(100)))];
        [self.headImg autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
        [self.headImg autoPinEdgeToSuperviewEdge:(ALEdgeTop)];
        
        UIImageView *nameImv = [UIImageView newAutoLayoutView];
//        [nameImv addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickName)]];
        nameImv.userInteractionEnabled = YES;
        nameImv.image = [UIImage imageNamed:@"rename.png"];
        nameImv.contentMode = UIViewContentModeScaleAspectFill;
        [_headView addSubview:nameImv];
        [nameImv autoSetDimensionsToSize:(CGSizeMake(Width_Real(15), Height_Real(15)))];
        self.nameLab = [[UILabel alloc] init];
//        [self.nameLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickName)]];
        self.nameLab.userInteractionEnabled = YES;
//        self.nameLab.text = [DataManager shareManager].dataModel.name.length > 0 ? [DataManager shareManager].dataModel.name : @"未命名";
        self.nameLab.textColor = [UIColor whiteColor];
        self.nameLab.font = [AppFont getFont_17];
        [_headView addSubview:self.nameLab];
        [self.nameLab autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
        [self.nameLab autoPinEdge:(ALEdgeTop) toEdge:(ALEdgeBottom) ofView:self.headImg withOffset:Y_Real(15)];
        [self.nameLab autoAlignAxis:(ALAxisHorizontal) toSameAxisOfView:nameImv];
        [self.nameLab autoPinEdge:(ALEdgeLeft) toEdge:(ALEdgeRight) ofView:nameImv withOffset:X_Real(4)];
        
        self.timeLab = [[UIButton alloc] init];
//        [self.timeLab setTitle:@"剩餘時間：360天" forState:(UIControlStateNormal)];
        [self.timeLab setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        self.timeLab.titleLabel.font = [AppFont getFont_14];
        [self.timeLab setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.15]];
        self.timeLab.layer.cornerRadius = Width_Real(11);
        self.timeLab.layer.borderWidth = Width_Real(0.7);
        self.timeLab.layer.borderColor = [UIColor whiteColor].CGColor;
//        [_headView addSubview:self.timeLab];
//        [self.timeLab autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
//        [self.timeLab autoPinEdge:(ALEdgeTop) toEdge:(ALEdgeBottom) ofView:self.nameLab withOffset:Y_Real(15)];
//        [self.timeLab autoSetDimension:(ALDimensionHeight) toSize:Width_Real(22)];
//        [self.timeLab autoSetDimension:(ALDimensionWidth) toSize:Width_Real(150)];
    }
    return _headView;
}

- (UIView *)footView {
    if (!_footView) {
        _footView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenW, Height_Real(150)))];
        
        UIButton *btn = [[UIButton alloc] init];
        [btn setBackgroundColor:[AppColor getColor_MAIN]];
        [btn setTitle:@"退出登录" forState:(UIControlStateNormal)];
        [btn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        btn.titleLabel.font = [AppFont getFont_16];
        btn.layer.cornerRadius = Width_Real(25);
        [btn addTarget:self action:@selector(clickLogOut) forControlEvents:(UIControlEventTouchUpInside)];
        [_footView addSubview:btn];
        [btn autoAlignAxisToSuperviewAxis:(ALAxisHorizontal)];
        [btn autoAlignAxisToSuperviewAxis:(ALAxisVertical)];
        [btn autoSetDimension:(ALDimensionHeight) toSize:Width_Real(50)];
        [btn autoSetDimension:(ALDimensionWidth) toSize:Width_Real(180)];
    }
    return _footView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
